package modele;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.Collection;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    private Pays p;
    
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }
    
    public void destroy() {
        Connect.close();
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page;
        String departement = request.getParameter("choixdep");
        String nom = request.getParameter("choixnom");
        String spe = request.getParameter("choixspe");
        String uc = request.getParameter("uc");
        switch (uc) {
            default:
                page = "index.jsp";
            break;
                
            case "dep":
                if (departement == null) {
                    Collection <Dep> lesDeps = p.getLesDeps();
                    request.setAttribute("lesDeps", lesDeps);
                    
                    page = "choixDep.jsp";
                } else {
                    Dep leDep = p.getLeDep(departement);
                    request.setAttribute("leDep", leDep);
                    
                    Collection <Med> lesMeds = leDep.getLesMeds();
                    request.setAttribute("lesMeds", lesMeds);
                    
                    page = "listeDep.jsp";
                }
            break;

            case "nom":
                if (nom == null) {
                    page = "choixNom.jsp";
                } else {
                    Collection <Med> lesMeds = p.getLesNoms(nom);
                    request.setAttribute("lesMeds", lesMeds);
                    
                    page = "listeNom.jsp";
                }
            break;
                
            case "spe":
                if (spe == null) {
                    Collection <Spe> lesSpes = p.getLesSpes();
                    request.setAttribute("lesSpes", lesSpes);
                    
                    page = "choixSpe.jsp";
                } else {
                    Spe laSpe = p.getLaSpe(spe);
                    request.setAttribute("laSpe", laSpe);
                    
                    Collection <Med> lesMeds = laSpe.getLesMeds();
                    request.setAttribute("lesMeds", lesMeds);
                    
                    page = "listeSpe.jsp";
                }
            break;
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}